package cn.iocoder.lw.module.core.controller.admin.workorder;

import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.pojo.PageParam;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.framework.excel.core.util.ExcelUtils;
import cn.iocoder.lw.module.core.controller.admin.workorder.vo.workorder.*;
import cn.iocoder.lw.module.core.convert.workorder.CoreWorkOrderConvert;
import cn.iocoder.lw.module.core.dal.dataobject.workorder.CoreWorkOrderDO;
import cn.iocoder.lw.module.core.enums.workorder.WorkOrderStatusEnum;
import cn.iocoder.lw.module.core.service.workorder.CoreWorkOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 工单")
@RestController
@RequestMapping("/core/work-order")
@Validated
public class CoreWorkOrderController {

    @Resource
    private CoreWorkOrderService workOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建工单")
    @PreAuthorize("@ss.hasPermission('core:work-order:create')")
    public CommonResult<Long> createWorkOrder(@Valid @RequestBody CoreWorkOrderSaveReqVO createReqVO) {
        return success(workOrderService.createWorkOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单")
    @PreAuthorize("@ss.hasPermission('core:work-order:update')")
    public CommonResult<Boolean> updateWorkOrder(@Valid @RequestBody CoreWorkOrderSaveReqVO updateReqVO) {
        workOrderService.updateWorkOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:delete')")
    public CommonResult<Boolean> deleteWorkOrder(@RequestParam("id") Long id) {
        workOrderService.deleteWorkOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<CoreWorkOrderRespVO> getWorkOrder(@RequestParam("id") Long id) {
        CoreWorkOrderDO workOrder = workOrderService.getWorkOrder(id);
        return success(BeanUtils.toBean(workOrder, CoreWorkOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单分页")
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<PageResult<CoreWorkOrderRespVO>> getWorkOrderPage(@Valid CoreWorkOrderPageReqVO pageReqVO) {
        PageResult<CoreWorkOrderDO> pageResult = workOrderService.getWorkOrderPage(pageReqVO);
        return success(CoreWorkOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单列表")
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<CoreWorkOrderRespVO>> getWorkOrderList(@Valid CoreWorkOrderPageReqVO pageReqVO) {
        List<CoreWorkOrderRespVO> list = workOrderService.getWorkOrderList(pageReqVO);
        return success(list);
    }

    @GetMapping("/export")
    @Operation(summary = "导出工单 Excel")
    @PreAuthorize("@ss.hasPermission('core:work-order:export')")
    public void exportWorkOrder(@Valid CoreWorkOrderPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoreWorkOrderRespVO> list = workOrderService.getWorkOrderList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "工单数据.xls", "数据", CoreWorkOrderRespVO.class,
                BeanUtils.toBean(list, CoreWorkOrderRespVO.class));
    }

    @PostMapping("/assign")
    @Operation(summary = "派单")
    @PreAuthorize("@ss.hasPermission('core:work-order:assign')")
    public CommonResult<Boolean> assignWorkOrder(@Valid @RequestBody CoreWorkOrderAssignReqVO assignReqVO) {
        workOrderService.assignWorkOrder(assignReqVO.getId(), assignReqVO.getAssigneeId(),
                assignReqVO.getAssigneePhone(), assignReqVO.getAssigneeName());
        return success(true);
    }

    @PutMapping("/accept")
    @Operation(summary = "接单")
    @Parameter(name = "id", description = "工单编号", required = true)
    @Parameter(name = "assigneeId", description = "接单人ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:accept')")
    public CommonResult<Boolean> acceptWorkOrder(@RequestParam("id") Long id,
                                                 @RequestParam("assigneeId") Long assigneeId) {
        workOrderService.acceptWorkOrder(id, assigneeId);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新工单状态")
    @Parameter(name = "id", description = "工单编号", required = true)
    @Parameter(name = "status", description = "新状态", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:update')")
    public CommonResult<Boolean> updateWorkOrderStatus(@RequestParam("id") Long id,
                                                       @RequestParam("status") Integer status) {
        workOrderService.updateWorkOrderStatus(id, status);
        return success(true);
    }

    @PutMapping("/complete")
    @Operation(summary = "完成工单")
    @PreAuthorize("@ss.hasPermission('core:work-order:complete')")
    public CommonResult<Boolean> completeWorkOrder(@Valid @RequestBody CoreWorkOrderCompleteReqVO completeReqVO) {
        workOrderService.completeWorkOrder(completeReqVO.getId(), completeReqVO);
        return success(true);
    }

    @GetMapping("/project/{projectId}")
    @Operation(summary = "根据项目编号获取工单列表")
    @Parameter(name = "projectId", description = "项目编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<CoreWorkOrderRespVO>> getWorkOrderListByProjectId(@PathVariable("projectId") Long projectId) {
        List<CoreWorkOrderDO> list = workOrderService.getWorkOrderListByProjectId(projectId);
        return success(BeanUtils.toBean(list, CoreWorkOrderRespVO.class));
    }

    @GetMapping("/house/{houseId}")
    @Operation(summary = "根据房源编号获取工单列表")
    @Parameter(name = "houseId", description = "房源编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<CoreWorkOrderRespVO>> getWorkOrderListByHouseId(@PathVariable("houseId") Long houseId) {
        List<CoreWorkOrderDO> list = workOrderService.getWorkOrderListByHouseId(houseId);
        return success(BeanUtils.toBean(list, CoreWorkOrderRespVO.class));
    }

    @GetMapping("/device/{deviceId}")
    @Operation(summary = "根据设备编号获取工单列表")
    @Parameter(name = "deviceId", description = "设备编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<CoreWorkOrderRespVO>> getWorkOrderListByDeviceId(@PathVariable("deviceId") Long deviceId) {
        List<CoreWorkOrderDO> list = workOrderService.getWorkOrderListByDeviceId(deviceId);
        return success(BeanUtils.toBean(list, CoreWorkOrderRespVO.class));
    }

    @GetMapping("/initiator/{initiatorId}")
    @Operation(summary = "根据发起人编号获取工单列表")
    @Parameter(name = "initiatorId", description = "发起人编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<CoreWorkOrderRespVO>> getWorkOrderListByInitiatorId(@PathVariable("initiatorId") Long initiatorId) {
        List<CoreWorkOrderDO> list = workOrderService.getWorkOrderListByInitiatorId(initiatorId);
        return success(BeanUtils.toBean(list, CoreWorkOrderRespVO.class));
    }

    @GetMapping("/assignee/{assigneeId}")
    @Operation(summary = "根据接单人编号获取工单列表")
    @Parameter(name = "assigneeId", description = "接单人编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<CoreWorkOrderRespVO>> getWorkOrderListByAssigneeId(@PathVariable("assigneeId") Long assigneeId) {
        List<CoreWorkOrderDO> list = workOrderService.getWorkOrderListByAssigneeId(assigneeId);
        return success(BeanUtils.toBean(list, CoreWorkOrderRespVO.class));
    }

    // ==================== 工单状态枚举 ====================

    @GetMapping("/status-list")
    @Operation(summary = "获得工单状态列表")
    @PreAuthorize("@ss.hasPermission('core:work-order:query')")
    public CommonResult<List<WorkOrderStatusEnum>> getWorkOrderStatusList() {
        return success(List.of(WorkOrderStatusEnum.values()));
    }

}